package com.fujitsu.cn.fnst.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.fujitsu.cn.fnst.base.BaseDao;
import com.fujitsu.cn.fnst.entiy.User;
import com.fujitsu.cn.fnst.exception.NotOneException;

@Repository
public class UserDao extends BaseDao<User> {

    public User findUser(String userId) {
        Session session = getSession();
        Criteria criteria = session.createCriteria(User.class).add(
                Restrictions.eq("workerId", userId));
        User user = null;
        try {
            user = query4One(criteria);
        } catch (NotOneException e) {
        } finally {
            releaseSession(session);
        }
        return user;
    }

    public User queryByWorkerId(String workerId) {
        Session session = getSession();
        Criteria criteria = session.createCriteria(User.class);
        criteria.add(Restrictions.eq("workerId", workerId));
        try {
            User user = query4One(criteria);
            return user;
        } catch (NotOneException e) {
            return null;
        } finally {
            releaseSession(session);
        }
    }

    public List<User> getAllUsers() {
        return query4Page(User.class, null, null, false, 0, 10000);
    }

	public String[] getAllEmails() {
		Session session=getSession();
		String sql="select DISTINCT email from User where state=1";
		 Query query = session.createQuery(sql);
		List<String>emailsList= query.list();
		String[] emails=new String[]{};
		emails=emailsList.toArray(emails);
		releaseSession(session);
		return emails;
	}

}
